#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define NAME_LEN 21
#define ID_LEN  11

struct Student
{
	char name[NAME_LEN];
	char ID[ID_LEN];
	char gender;
};

typedef struct Student Stu;

int FindByNameOrIDAndJudge(char key1[], char key2[], Stu* base, int size)
{
	int Peo1_pos = 0, Peo2_pos = 0;

	//find
	for (int i = 0; i < size; i++)
	{
		if (!strcmp(base[i].name, key1) || !strcmp(base[i].ID, key1))
		{
			Peo1_pos = i;
		}

		if(!strcmp(base[i].name, key2) || !strcmp(base[i].ID, key2))
		{
			Peo2_pos = i;
		}
	}

	//judge
	if (base[Peo1_pos].gender != base[Peo2_pos].gender)
	{
		return 1;
	}

	return 0;
}

int main()
{
	int n, m;
	scanf("%d", &n);

	Stu* student = (Stu*)malloc(sizeof(Stu) * n);

	for (int i = 0; i < n; i++)
	{
		scanf("%s %s %c", student[i].name, student[i].ID, &student[i].gender);
	}

	char key1[NAME_LEN] = { 0 }, key2[NAME_LEN] = { 0 };

	scanf("%d", &m);
	for (int i = 0; i < m; i++)
	{
		scanf("%s %s", key1, key2);

		if (FindByNameOrIDAndJudge(key1, key2, student, n))
		{
			printf("Y\n");
		}
		else
		{
			printf("N\n");
		}
	}

	free(student);
	student = NULL;

	return 0;
}
